<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="工艺编码" prop="operationCode">
        <el-input
          v-model="queryParams.operationCode"
          disabled
          placeholder="请输入工艺编码"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工步序号" prop="stepNum">
        <el-input
          v-model="queryParams.stepNum"
          placeholder="请输入工步序号"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工步名称" prop="stepName">
        <el-input
          v-model="queryParams.stepName"
          placeholder="请输入工步名称"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工步编码" prop="stepCode">
        <el-input
          v-model="queryParams.stepCode"
          placeholder="请输入工步编码"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作方式" prop="operationMode">
        <el-select
          v-model="queryParams.operationMode"
          placeholder="请选择工作方式"
          clearable
          size="small"
          style="width: 200px"
        >
          <el-option
            v-for="dict in operationModeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="节拍时间" prop="beatTime">
        <el-input
          v-model="queryParams.beatTime"
          placeholder="请输入节拍时间"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:steps:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:steps:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:steps:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:steps:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="stepsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false" />
      <el-table-column label="工步序号" align="center" prop="stepNum" />
      <el-table-column label="工步名称" align="center" prop="stepName" />
      <el-table-column label="工步编码" align="center" prop="stepCode" />
      <el-table-column label="工步类型" align="center" prop="stepType" />
      <el-table-column label="工步内容" align="center" prop="stepContent" />
      <el-table-column label="技术要求" align="center" prop="requirement" />
      <el-table-column label="工艺编码" align="center" prop="operationCode" />
      <el-table-column label="工作方式" align="center" prop="operationMode" :formatter="operationModeFormat" />
      <el-table-column label="节拍时间(S)" align="center" prop="beatTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="margin-right: 9px"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:steps:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:steps:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工步对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="工步编码" prop="stepCode">
              <el-input
                v-model="form.stepCode"
                placeholder="请输入工步编码"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工步序号" prop="stepNum">
              <el-input
                v-model="form.stepNum"
                placeholder="请输入工步序号"
                style="width: 240px"
                type="number"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工步名称" prop="stepName">
              <el-input
                v-model="form.stepName"
                placeholder="请输入工步名称"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工步内容" prop="stepContent">
              <el-input
                v-model="form.stepContent"
                placeholder="请输入工步内容"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工步类型" prop="stepType">
              <el-select
                v-model="form.stepType"
                placeholder="请选择工步类型"
                style="width: 240px"
                @change="stepTypeChange"
              >
                <el-option
                  v-for="dict in stepTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                >
                  <span style="float: left">{{ dict.dictValue }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.dictLabel }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工艺编码" prop="operationCode">
              <el-input
                v-model="form.operationCode"
                placeholder="请输入工艺编码"
                style="width: 240px"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工作方式" prop="operationMode">
              <el-select
                v-model="form.operationMode"
                placeholder="请选择工作方式"
                style="width: 240px"
              >
                <el-option
                  v-for="dict in operationModeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="节拍时间" prop="beatTime">
              <el-input
                v-model="form.beatTime"
                placeholder="请输入节拍时间"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item :label="typeTitle" prop="groupName">
              <el-select
                v-model="form.groupName"
                placeholder="请选择（多选）"
                style="width: 240px"
                multiple
              >
                <el-option
                  v-for="dict in groupNameOptions"
                  :key="dict.id"
                  :label="dict.materialName"
                  :value="dict.id"
                >
                  <span style="float: left">{{ dict.id }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.materialName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="技术要求" prop="requirement">
              <el-input
                v-model="form.requirement"
                type="textarea"
                :rows="4"
                placeholder="请输入内容"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSteps,
  getSteps,
  getStepsScan,
  getStepsOther,
  delSteps,
  addSteps,
  updateSteps,
  exportSteps,
} from "@/api/base/steps";
import Vue from 'vue'
export default {
  name: "Steps",
  data() {
    return {
      //更新标识 1
      IsUpdate:0,
      NowOperations: "",
      NowStationCode: "",
      typeTitle: '关重件选择',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工步表格数据
      stepsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        operationCode: undefined,
        stepNum: undefined,
        stepName: undefined,
        stepCode: undefined,
        operationMode: undefined,
        beatTime: undefined
      },
      // 工作方式字典
      operationModeOptions: [],
      // 工步类型字典
      stepTypeOptions: [],
      // 数据采集项
      groupNameOptions: [],
      collectOptions: [],
      KesOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stepNum: [{ required: true, message: "工步序号不能为空", trigger: "blur" }],
        stepName: [{ required: true, message: "工步名称不能为空", trigger: "blur"}],
        stepCode: [{ required: true, message: "工步编码不能为空", trigger: "blur" }],
        stepContent: [{ required: true, message: "工步内容不能为空", trigger: "blur" }],
        operationCode: [{ required: true, message: "工艺编码不能为空", trigger: "blur" }],
        operationMode: [{ required: true, message: "工作方式不能为空", trigger: "change" }]
      },
    };
  },
  created() {
    this.NowStationCode = this.$route.params && this.$route.params.stationCode;
    this.NowOperations = this.$route.params && this.$route.params.operationCode;
    this.queryParams.operationCode = this.NowOperations;
    this.getDicts("mes_work_type").then((response) => {
      this.operationModeOptions = response.data;
    });
    this.getDicts("step_type").then((response) => {
      this.stepTypeOptions = response.data;
    });
    getStepsScan(this.NowOperations).then((response) => {
      this.KesOptions = response.data;
    });
    getStepsOther(this.NowOperations).then((response) => {
      response.data.map((item, index) => {
        item.materialName = item.collectItemName;
        item.id = item.collectItemId;
      });
      this.collectOptions = response.data;
    });
    this.getList();
  },
  methods: {
    // 工作方式字典翻译
    operationModeFormat(row, column) {
      return this.selectDictLabel(this.operationModeOptions, row.operationMode);
    },
    /** 查询工步列表 */
    getList() {
      this.loading = true;
      listSteps(this.queryParams).then((response) => {
        this.stepsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 工步类型改变时
    stepTypeChange(value) {
      // this.groupNameOptions = [];
      // this.form.groupName = undefined;
      // this.form.groupName=[]
      if (value === "1") {
          this.typeTitle = '关重件选择';
          this.groupNameOptions = this.KesOptions;
          this.form.groupName = [];
      } else {
          this.typeTitle = '数据采集项';
          this.groupNameOptions = this.collectOptions;
          this.form.groupName = [];
      }
      this.$forceUpdate();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        operationCode: this.NowOperations,
        groupName: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.operationCode = this.NowOperations;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.groupNameOptions = []
      this.title = "添加工步";
    },
    /** 修改按钮操作 */
   async handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
        await  getSteps(id).then((response) => {
        this.form = response.data;
      });
      // await this.stepTypeChange(this.form.stepType)
        if (this.form.stepType === "1") {
          this.typeTitle = '关重件选择';
          this.groupNameOptions = this.KesOptions;
          if(this.IsUpdate=1){
            this.form.mesQualityItemsIdsz = [];
          }
          this.$set(this.form, 'groupName', this.form.mesScanKeysRelationIdsz)
        } else {
          this.typeTitle = '数据采集项';
          this.groupNameOptions = this.collectOptions;
          if(this.IsUpdate=1){
            this.form.mesScanKeysRelationIdsz = [];
          }
          this.$set(this.form, 'groupName', this.form.mesQualityItemsIdsz)
        };
      this.open = true;
      this.title = "修改工步";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.stepType === "1") {
            this.form.mesScanKeysRelationIdsz = this.form.groupName;
            this.form.mesQualityItemsIdsz = [];
          } else {
            this.form.mesQualityItemsIdsz = this.form.groupName;
            this.form.mesScanKeysRelationIdsz = [];
          };
          if (this.form.id != null) {
            updateSteps(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addSteps(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除工步编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delSteps(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有工步数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportSteps(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>
